home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / phoonsrc / original / xphoon.c < prev    next >
C/C++ Source or Header  |  1992-07-04  |  8KB  |  309 lines

  1. #ifndef lint
  2. static char rcsid[] =
  3.     "@(#) $Header: /src/X11/contrib/demos/xphoon/RCS/xphoon.c,v 1.4 90/06/27 10:31:31 tim Exp $ (LBL)";
  4. #endif
  5.  
  6. /*
  7. ** Copyright (C) 1988 by Jef Poskanzer and Craig Leres.
  8. **
  9. ** Permission to use, copy, modify, and distribute this software and its
  10. ** documentation for any purpose and without fee is hereby granted, provided
  11. ** that the above copyright notice appear in all copies and that both that
  12. ** copyright notice and this permission notice appear in supporting
  13. ** documentation.  This software is provided "as is" without express or
  14. ** implied warranty.
  15. */
  16.  
  17. #include <X11/Xos.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/Xatom.h>
  21. #define BitmapSize(w,h) ((((w)+7)/8)*(h))
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include "tws.h"
  25.  
  26. char *argv0;
  27. int blackflag = 0;
  28. int demoflag = 0;
  29. Display *dpy = NULL;
  30.  
  31. char *malloc();
  32.  
  33. main(argc, argv)
  34.     int argc;
  35.     char **argv;
  36. {
  37.     char *display = NULL;
  38.     int w, h, cx, cy, r;
  39.     int size;
  40.     char *bits, *xbits;
  41.     int delayminutes = 0;
  42.  
  43.     argv0 = argv[0];
  44.  
  45. again:
  46.     if (argc > 1 && strcmp(argv[1], "-b") == 0) {
  47.         argv++;
  48.         argc--;
  49.         blackflag = 1;
  50.         goto again;
  51.     }
  52.     if (argc > 1 && strcmp(argv[1], "-d") == 0) {
  53.         argv++;
  54.         argc--;
  55.         demoflag = 1;
  56.         goto again;
  57.     }
  58.     if (argc > 2 && strcmp(argv[1], "-t") == 0) {
  59.         argv++;
  60.         argc--;
  61.         if (sscanf(argv[1], "%d", &delayminutes) != 1)
  62.             goto usage;
  63.         argv++;
  64.         argc--;
  65.         goto again;
  66.     }
  67.     if (argc > 2 && strcmp(argv[1], "-x") == 0) {
  68.         argv++;
  69.         argc--;
  70.         if (sscanf(argv[1], "%d", &cx) != 1)
  71.             goto usage;
  72.         argv++;
  73.         argc--;
  74.         goto again;
  75.     }
  76.     if (argc > 2 && strcmp(argv[1], "-y") == 0) {
  77.         argv++;
  78.         argc--;
  79.         if (sscanf(argv[1], "%d", &cy) != 1)
  80.             goto usage;
  81.         argv++;
  82.         argc--;
  83.         goto again;
  84.     }
  85.     if (argc > 2 && strcmp(argv[1], "-display") == 0) {
  86.         argv++;
  87.         argc--;
  88.         display = argv[1];
  89.         argv++;
  90.         argc--;
  91.         goto again;
  92.     }
  93.  
  94.     if (argc > 1) {
  95. usage:
  96.         fprintf(stderr,
  97.             "usage: %s [-b] [-t minutes] [-display display]\n", argv0);
  98.         exit(1);
  99.         
  100.     }
  101.  
  102.     if ((dpy = XOpenDisplay(display)) == 0) {
  103.         fprintf(stderr, "%s: Can't open display \"%s\"\n",
  104.             argv0, XDisplayName(display));
  105.         exit(1);
  106.     }
  107.  
  108.     w = DisplayWidth(dpy, DefaultScreen(dpy));
  109.     h = DisplayHeight(dpy, DefaultScreen(dpy));
  110.     checkbitmapsize(&w, &h);
  111.     size = BitmapSize(w, h);
  112.     bits = (char *)malloc(size);
  113.  
  114.     getbitmap(w, h, bits, &cx, &cy, &r);
  115.  
  116.     if (delayminutes <= 0 && ! demoflag) {
  117.         hackbits(dtwstime(), w, h, bits, cx, cy, r);
  118.         setroot(w, h, bits);
  119.         XCloseDisplay(dpy);
  120.         exit(0);
  121.     }
  122.  
  123.     xbits = (char *)malloc(size);
  124.     for (;;) {
  125.         bcopy((char *)bits, (char *)xbits, size);
  126.         hackbits(dtwstime(), w, h, xbits, cx, cy, r);
  127.         setroot(w, h, xbits);
  128.         if ( demoflag )
  129.             sleep(1);  /* continuous mode */
  130.         else
  131.             sleep(delayminutes * 60);
  132.     }
  133.     /* NOTREACHED */
  134. }
  135.  
  136.  
  137. setroot(w, h, bits)
  138.     int w, h;
  139.     char *bits;
  140. {
  141.     Pixmap bitmap;
  142.     Pixmap pixmap;
  143.     GC gc;
  144.     XGCValues gcv;
  145.     unsigned long length, after;
  146.     int format;
  147.     Atom prop, type;
  148.     unsigned char *data;
  149.  
  150.     bitmap = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), bits, w, h);
  151.     if (bitmap == 0) {
  152.         fprintf(stderr, "%s: Unable to store Bitmap", argv0);
  153.         exit(1);
  154.     }
  155.  
  156.     gcv.foreground = BlackPixel(dpy,DefaultScreen(dpy));
  157.     gcv.background = WhitePixel(dpy,DefaultScreen(dpy));
  158.     gc = XCreateGC(
  159.         dpy, DefaultRootWindow(dpy), GCForeground|GCBackground, &gcv);
  160.  
  161.     pixmap = XCreatePixmap(
  162.         dpy, DefaultRootWindow(dpy), w, h,
  163.         DefaultDepth(dpy, DefaultScreen(dpy)));
  164.     if (pixmap == 0) {
  165.         fprintf(stderr, "%s: Unable to create Pixmap", argv0);
  166.         exit(1);
  167.     }
  168.  
  169.     XCopyPlane(dpy, bitmap, pixmap, gc, 0, 0, w, h, 0, 0, 1L);
  170.     XSetWindowBackgroundPixmap(dpy, DefaultRootWindow(dpy), pixmap);
  171.     XFreeGC(dpy, gc);
  172.     XFreePixmap(dpy, bitmap);
  173.     XFreePixmap(dpy, pixmap);
  174.     XClearWindow(dpy, DefaultRootWindow(dpy));
  175.     XFlush(dpy);
  176.  
  177.     /* Not sure what this garbage does, but xsetroot has it, so... */
  178.     prop = XInternAtom(dpy, "_XSETROOT_ID", False);
  179.     (void) XGetWindowProperty(
  180.         dpy, DefaultRootWindow(dpy), prop, 0L, 1L, True, AnyPropertyType,
  181.         &type, &format, &length, &after, &data);
  182.     if ((type == XA_PIXMAP) && (format == 32) &&
  183.         (length == 1) && (after == 0))
  184.         XKillClient(dpy, *((Pixmap *)data));
  185.     else if (type != None)
  186.         fprintf(stderr, "%s: warning: _XSETROOT_ID property is garbage\n",
  187.             argv0);
  188.     XFlush(dpy);
  189. }
  190.  
  191.  
  192. #ifdef notdef
  193. short leftmask[16] = {
  194.     0xffff, 0xfffe, 0xfffc, 0xfff8, 0xfff0, 0xffe0, 0xffc0, 0xff80,
  195.     0xff00, 0xfe00, 0xfc00, 0xf800, 0xf000, 0xe000, 0xc000, 0x8000,
  196. };
  197. short rightmask[16] = {
  198.     0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f,
  199.     0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff,
  200. };
  201. #endif notdef
  202. static char  leftmask[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
  203. static char rightmask[8] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
  204.  
  205. static char  shade_0_bits[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  206. static char  shade_1_bits[] = {0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  207. static char  shade_2_bits[] = {0xfe,0xff,0xfb,0xff,0xff,0xff,0xff,0xff};
  208. static char  shade_3_bits[] = {0xfe,0xff,0xfb,0xff,0x7f,0xff,0xff,0xff};
  209. static char  shade_4_bits[] = {0xfe,0xff,0xfb,0xff,0x7f,0xff,0xff,0xef};
  210. static char  shade_5_bits[] = {0xfe,0xbf,0xfb,0xff,0x7f,0xff,0xff,0xef};
  211. static char  shade_6_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xff,0xff,0xef};
  212. static char  shade_7_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xff,0xfe,0xef};
  213. static char  shade_8_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7f,0xdf,0xfe,0xef};
  214. static char  shade_9_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfe,0xef};
  215. static char shade_10_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfa,0xef};
  216. static char shade_11_bits[] = {0xfe,0xbf,0xfb,0xdf,0x7d,0xdf,0xfa,0xaf};
  217. static char shade_12_bits[] = {0xfe,0xbf,0xfa,0xdf,0x7d,0xdf,0xfa,0xaf};
  218. static char shade_13_bits[] = {0xfe,0xaf,0xfa,0xdf,0x7d,0xdf,0xfa,0xaf};
  219. static char shade_14_bits[] = {0xfe,0xaf,0xfa,0xdf,0x75,0xdf,0xfa,0xaf};
  220. static char shade_15_bits[] = {0xfa,0xaf,0xfa,0xdf,0x75,0xdf,0xfa,0xaf};
  221.  
  222. char *shades[16] = {
  223.     shade_0_bits,  shade_1_bits,  shade_2_bits,  shade_3_bits,
  224.     shade_4_bits,  shade_5_bits,  shade_6_bits,  shade_7_bits,
  225.     shade_8_bits,  shade_9_bits,  shade_10_bits, shade_11_bits,
  226.     shade_12_bits, shade_13_bits, shade_14_bits, shade_15_bits };
  227.  
  228. #define PI 3.14159265358979323846  /* Assume not near black hole or in
  229.                       Tennessee */
  230.  
  231. double jtime(), phase();
  232.  
  233. hackbits(t, w, h, bits, cx, cy, r)
  234.     struct tws *t;
  235.     int w, h;
  236.     char *bits;
  237.     int cx, cy, r;
  238. {
  239.     double jd, angphase, cphase, aom, cdist, cangdia, csund, csuang;
  240.     int i;
  241.     register int x, y;
  242.     int xleft, xright;
  243.     double fxleft, fxright;
  244.     double fy;
  245.     int wxright, bxright, wxleft, bxleft;
  246.     int off;
  247.     double cap, ratio;
  248.     int shadeindex;
  249.     char shade;
  250.     static double demoinc = 0.0;
  251.  
  252.     jd = jtime( t );
  253.     if ( demoflag ) {
  254.         /* Jump ahead a day each time through. */
  255.         jd += demoinc;
  256.         demoinc += 1.0;
  257.     }
  258.  
  259.     angphase = phase( jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  260.     cap = cos( angphase );
  261.  
  262.     /* Hack to figure approximate earthlighting. */
  263.     if ( cphase < 0.1 ) cphase = 0.1;
  264.     if ( cphase > 0.9 ) cphase = 0.9;
  265.     ratio = (1.0 - cphase) / cphase;  /* ratio varies from 9.0 to 0.111 */
  266.     shadeindex = (int) ( ratio / 9.0 * 15.9999 );
  267.  
  268. #ifdef DEBUG
  269. printf("angphase %f, cap %f\n", angphase, cap);
  270. #endif
  271.  
  272.     for (i = 0; i < 2 * r; i++) {
  273.         y = cy - r + i;
  274.         fy = i - r;
  275.         fxright = r * sqrt(1.0 - (fy * fy) / (r * r));
  276.         fxleft = - fxright;
  277.         if (angphase >= 0.0 && angphase < PI)
  278.             fxright *= cap;
  279.         else
  280.             fxleft *= cap;
  281.  
  282.         xright = fxright + cx;
  283.         xleft = fxleft + cx;
  284.  
  285.         wxright = xright / 8;
  286.         bxright = xright % 8;
  287.  
  288.         wxleft = xleft / 8;
  289.         bxleft = xleft % 8;
  290.  
  291.         off = y * ((w + 7) / 8);
  292.  
  293.         if ( blackflag )
  294.             shade = 0xff;
  295.         else
  296.             shade = shades[shadeindex][y % 8];
  297.         if (wxleft == wxright)
  298.             bits[wxleft + off] |=
  299.                 leftmask[bxleft] & shade & rightmask[bxright];
  300.         else {
  301.             bits[wxleft + off] |= leftmask[bxleft] & shade;
  302.             for (x = wxleft + 1; x < wxright; x++)
  303.                 bits[x + off] |= shade;
  304.             bits[wxright + off] |= rightmask[bxright] & shade;
  305.         }
  306.     }
  307.     
  308. }
  309.